package com.example.kechengsheji;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;

public class shouyin extends Activity {
   private EditText rshangpin,bnum;
   private Button next,total,delete,sum,jiesuan;
    String name1;
    private TextView tv_showshangpin,tv_showmoney,tv_shownum,tv_sum,tv_total,zshangpin,zmoney,znum,zsum;
    final DatabaseHelper dbHelper=new DatabaseHelper(shouyin.this);




    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.shouyin);


        next = findViewById(R.id.next);

        total=findViewById(R.id.total);
        rshangpin = findViewById(R.id.rshangpin);
        bnum = findViewById(R.id.bnum);
        tv_sum=findViewById(R.id.tv_sum);
        tv_showshangpin = findViewById(R.id.tv_showshangpin);
        tv_showmoney = findViewById(R.id.tv_showmoney);
        tv_shownum=findViewById(R.id.tv_shownum);
        tv_total=findViewById(R.id.tv_total);
        zshangpin=findViewById(R.id.zshangpin);
        zmoney = findViewById(R.id.zmoney);
        znum = findViewById(R.id.znum);
        zsum=findViewById(R.id.zsum);



        ArrayList<Integer> myNumbers = new ArrayList<Integer>();



        next.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name1 = rshangpin.getText().toString();

//                shownum.setText(null);
//                showmoney.setText(null);
                if (name1.equals("")) {

                    Cursor cursor = db.rawQuery("select * from information", null);



                } else {
                    Cursor cursor = db.rawQuery("select * from information where bshangpin=?", new String[]{name1});
                    while (cursor.moveToNext()) {
                        String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
                        int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
//                        tv_showshangpin.setText(null);
                        tv_showshangpin.setText(tv_showshangpin.getText() + "\n" + newshangpin);
                        zshangpin.setText(zshangpin.getText() + "\n" + newshangpin);
//                        tv_showmoney.setText(null);
                        tv_showmoney.setText(tv_showmoney.getText() + "\n" + newmoney);
                        zmoney.setText(zmoney.getText() + "\n" + newmoney);
                        //插入数量
                        String newnum = bnum.getText().toString();
//                        tv_shownum.setText(null);
                        tv_shownum.setText(tv_shownum.getText() + "\n" + newnum);
                        znum.setText(znum.getText() + "\n" + newnum);


                        int i = Integer.valueOf(newnum).intValue();//商品数量
                        int heji = newmoney * i;//单个商品的合计
//                        tv_sum.setText(null);
                        tv_sum.setText(tv_sum.getText() + "\n" + heji);
                        zsum.setText(zsum.getText() + "\n" + heji);
                        myNumbers.add(heji);


                        //            jshangpin VARCHAR(20),jmoney INTEGER,jnum INTEGER,
//            jsum INTEGER,jyingfu INTEGER,jshifu INTEGER,jzhaoling INTEGER)
                        final shouyinhelp bdHelper=new shouyinhelp(shouyin.this);
                        SQLiteDatabase bd=bdHelper.getWritableDatabase();
                        ContentValues values=new ContentValues();
                        values.put("jshangpin",zshangpin.getText().toString());
                        values.put("jmoney",zmoney.getText().toString());
                        values.put("jnum",znum.getText().toString());
                        values.put("jsum",zsum.getText().toString());
                        long id =bd.insert("information",null,values);
                        Log.d("myDeBug","insert");
                        zshangpin.setText(null);
                        zmoney.setText(null);
                        znum.setText(null);
                        zsum.setText(null);
                        }
                        bnum.setText(null);
                        rshangpin.setText(null);
                    }

                }


        });


        total.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer sum = 0;
               for (Integer tmp : myNumbers) {
                       sum = tmp + sum;
                   }
                tv_total.setText(null);
                tv_total.setText(tv_total.getText()+"\t"+sum);

            }
        });

        jiesuan =(Button) findViewById(R.id.jiesuan);
        jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                  Intent intent=new Intent(shouyin.this,jiesuan.class);
                  intent.putExtra("ntotal",tv_total.getText().toString());
                  intent.putExtra("nshangpin",tv_showshangpin.getText().toString());
                  intent.putExtra("ndanjia",tv_showmoney.getText().toString());
                  intent.putExtra("nshuliang",tv_shownum.getText().toString());
                  intent.putExtra("nsum",tv_sum.getText().toString());


                  intent.setClass(shouyin.this, jiesuan.class);//从哪里跳到哪里
                  shouyin.this.startActivity(intent);
                  startActivity(intent);

            }
        });
    }


    public void myShow(){
        final DatabaseHelper dbHelper=new DatabaseHelper(shouyin.this);
    SQLiteDatabase db=dbHelper.getReadableDatabase();

    tv_showshangpin.setText("商品");
    tv_showmoney.setText("单价");
    Cursor cursor = db.rawQuery("select * from information",null);
    while (cursor.moveToNext()) {
        String newshangpin = cursor.getString(cursor.getColumnIndex("bshangpin"));
        int newmoney = cursor.getInt(cursor.getColumnIndex("bmoney"));
        tv_showshangpin.setText(tv_showshangpin.getText() + "\n" + newshangpin);
        tv_showmoney.setText(tv_showmoney.getText()+"\n" + newmoney);
    }
    cursor.close();
    db.close();
}

}







